package com.sgb.kjwl.view.ui.activity.material.hall;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickNoticeFragmentModle extends BaseViewModel {

    private PickNoticeFragment fragment;
    private HomeRepertory repertory;
    public ObservableField<Integer> page = new ObservableField<>(1);
    public ObservableField<Integer> prepage = new ObservableField<>(1);
    private int pageSize = 10;
    public int total;

    public PickNoticeFragmentModle(PickNoticeFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //招标中列表
    public void getBiddingListData(String name,String type,String goodsType,String biddingMethod,String verticalType,String applyStartTime,String price,String areacode,String city,String sortMethod,String comNo){
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null,false);
        Map map = new HashMap();
        map.put("limit",pageSize);
        map.put("page",prepage.get());
        if(!TextUtils.isEmpty(name)){
            map.put("name",name);
        }
        if(!TextUtils.isEmpty(biddingMethod)){
            map.put("biddingMethod",biddingMethod);
        }
        map.put("verticalType",verticalType);
        if("0".equals(type)){
            type = "";
        }
        map.put("type",type);
        map.put("goodsType",goodsType);
        map.put("applyStartTime",applyStartTime);
        map.put("price",price);
        map.put("sortMethod",sortMethod);
        map.put("comNo",comNo);
        if(!TextUtils.isEmpty(areacode)){
            map.put("areacode",areacode);
        }
        if(!TextUtils.isEmpty(city)){
            map.put("city",city);
        }
        APIManager.getInstance().getPickAPI().getPreBiddingList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> call, Response<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    fragment.getBidedData(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> call, Throwable t) {
                MLog.d(AppConstant.API_LOG, t.getMessage());
            }
        });
    }


    public void getProvinceData() {
        Map map = new HashMap();
        map.put("areaCode", repertory.getAreaCode());
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null,false);
        APIManager.getInstance().getPickAPI().getLocationNew(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setProvinceDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData(String province, boolean isShowDialog) {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null,false);
        APIManager.getInstance().getPickAPI().getAreaSecond(province).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setCityDatas(response.body().getData(), isShowDialog);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //新增类型过滤接口
    public void getTenderGoodsCategoryAllList(String category) {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null,false);
        Map map = new HashMap();
        map.put("type", category);
        APIManager.getInstance().getPickAPI().getTenderGoodsCategoryAllList(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTenderGoodsCategoryAllList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getFilterTypeData() {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null,false);
        APIManager.getInstance().getPickAPI().tenderNewTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getSortTypeData() {
        APIManager.getInstance().getPickAPI().getTenderPreNoticeSortSelect().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setSortDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public void getPriceData() {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null,false);
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setPriceDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTenderNewTypeList() {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null,false);
        APIManager.getInstance().getPickAPI().getTenderNewTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTenderTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
